<?php
namespace app\common\model;
use think\Model;

/**
 * 评论
 */
class Comment extends Model {
	protected $insert = ['add_time'];

    protected function setAddTimeAttr(){
        return time();
    }

    /**
     * [getList 评论类表]
     * @param  array   $where [description]
     * @param  integer $p     [页码]
     * @param  integer $user_id    [当前用户id]
     * @return [type]         [description]
     */
    public function getList($where=[],$p=1,$user_id=0){
        $list = $this->alias('c')->join(['bz_user'=>'u'],'u.id = user_id','left')->field('c.id,user_id,content,zan,c.add_time as comment_time,u.nick_name,u.photo_path')->limit(($p-1)*config('page_size'),config('page_size'))->select();

        $zanModel = db('comment_zan');
        foreach($list as $key => $val){
            $list[$key]['is_zan'] = 0;
            //查看当前用户是否点过赞
            if($zanModel->where('comment_id',$val['id'])->where('user_id',$user_id)->find()){
                $list[$key]['is_zan'] = 1;
            }

            //评论时间格式化
            $list[$key]['commnet_time'] = timeFormat($val['commnet_time']);
        }

        return empty($list)?[]:$list;
    }


    
    
}
